import Vuex from 'vuex'
import Vue from 'vue'
import {getUserInfo, login, register,logout} from '@/api/login'

Vue.use(Vuex);
const store = new Vuex.Store({
    state: {
        id: '',
        account: '',
        name: '',
        avatar: '',
        token: localStorage.token,
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_ACCOUNT: (state, account) => {
            state.account = account
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ID: (state, id) => {
            state.id = id
        }
    },
    actions: {
        //用户登录
        login({commit}, user) {
            return new Promise((resolve, reject) => {
                login(user.account, user.password).then(data => {
                    const res = data.data;
                    console.log('res' + JSON.stringify(data.data))
                    if (res.success) {
                        commit('SET_TOKEN', res.data)
                        localStorage.token = res.data
                        resolve()
                    } else {
                        reject(res.msg)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //用户注册
        register({commit}, user) {
            return new Promise((resolve, reject) => {
                register(user.account, user.password).then(data => {
                    const res = data.data;
                    if (res.success) {
                        /*commit('SET_TOKEN', data.data)
                        localStorage.token=res.data*/
                        resolve()
                    } else {
                        reject(res.msg)
                    }
                }).catch((error) => {
                    reject(error)
                })
            })
        },
        //获取用户信息
        getUserInfo({commit}) {
            return new Promise((resolve, reject) => {
                getUserInfo().then(data => {
                    const res = data.data;
                    console.log(res.data)
                    if (res.success) {
                        commit('SET_ACCOUNT', res.data.username)
                        commit('SET_NAME', res.data.nickname)
                        commit('SET_AVATAR', res.data.userImg)
                        commit('SET_ID', res.data.userId)
                        resolve(res)
                    } else {
                        commit('SET_ACCOUNT', '')
                        commit('SET_NAME', '')
                        commit('SET_AVATAR', '')
                        commit('SET_ID', '')
                        commit('SET_TOKEN', '')
                        localStorage.token= ''
                        resolve(res)
                    }
                }).catch(error => {
                    reject(error)})
            })
        },
        //用户退出登录
        logout({commit}) {
            return new Promise((resolve, reject) => {
                logout().then(data => {
                    const res = data.data;
                    console.log(JSON.stringify(res))
                    if (res.success) {
                        commit('SET_ACCOUNT', '')
                        commit('SET_NAME', '')
                        commit('SET_AVATAR', '')
                        commit('SET_ID', '')
                        commit('SET_TOKEN', '')
                        localStorage.removeItem('token')
                        resolve(res.msg)
                    } else {
                        reject(res)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }

})
export default store